
#include "seive.h"
#include <omp.h>

int seive(unsigned int number)
{
    static unsigned int size = 0;
    static unsigned int *number_list;
    int nthreads = omp_get_max_threads();
	if(number & 0x1)
    {
        unsigned int root = (int) sqrt(number);
        int i,j;
        if(number > size)
        {
            size = number;
            number_list = calloc(sizeof(int),size);
            
            #pragma omp parallel for private(i,j) num_threads(nthreads)
            for(i = 1; i < root; i++)
            {	

                for(j = (i*2) + 1; j < size; j += (i+1))
                {
                    number_list[j] = 1;
                }
            }
        }
    
        return !number_list[number-1];
    }

    else if(number == 2)
        return 1;

    else
        return 0;
}

